Keras Introduction

What is Keras?

Keras is a deep learning API written in Python, running on top of the machine learning platform TensorFlow. It was developed with a focus on enabling fast experimentation. Being able to go from idea to result as fast as possible is key to doing good research.

See https://keras.io/about/ for detailed Keras guideline.

Environment

Our suggested coding environment setting is:

How can I install Keras?

Keras/TensorFlow are compatible with:

Keras comes packaged with TensorFlow 2.0 as tensorflow.keras. To start using Keras, simply install TensorFlow 2. Tensorflow is an open-source programming language developed by Google that is specifically designed to make programming deep-learning programs easy, or at least easier.

You can install Anaconda as environment manager.

Code Examples

Our textbook use Tensorflow 1. However, Tensorflow 2 and Keras is becoming more and more polular. We will use the Keras in Tensorflow 2 for project implementations of this course. However, a brief understanding of tensorflow 2 is necessary before you use Keras.

In the following, we give some examples in both Tensorflow 2 and Keras.

Hello World Example from Section 2.1

The example code in the textbook is in Tensorflow 1. We migrate it to Tensorflow 2.

A Simple Feedforward NN in Tensorflow 2.0 & Keras

Figure 2.2 in textbook: a simple feedforward NN on MNIST handwritting dataset.

Tensorflow 2 uses Keras layers and models to manage variables. The layers and models are two core data structures of Keras. The simplest type of model is the Sequential model, a linear stack of layers. For more complex architectures, you should use the Keras functional API, which allows to build arbitrary graphs of layers, or write models entirely from scratch via subclasssing.

Here is the Sequantial model:

Stacking layers is as easy as .add():

Prints a string summary of the network

Once your model looks good, configure its learning process with .compile():

Load MNIST dataset as an example

You can now iterate on your training data in batches:

Visualize the loss value during training

Evaluate your test loss and metrics in one line:

Or generate predictions on new data: